import StringTool from '@/utils/StringUtil'

/**
 * author : xuzongmeng
 * date   : 2018/9/29
 * desc   : function 方法声明
 */
export default class FunctionPrototypeConfig {
  static createPrototype (vue) {
    // 查找指定的元素在数组中的位置
    vue.prototype.indexOf = function (val) {
      for (let i = 0; i < this.length; i++) {
        if (this[i] === val) return i
      }
      return -1
    }
    /* 去除数组中的某个元素 */
    vue.prototype.remove = function (val) {
      let index = this.indexOf(val)
      if (index > -1) {
        this.splice(index, 1)
      }
    }
    vue.prototype.clearForm = function () {
      // vue.$refs.dataForm.resetFields();
      //   // this.$refs['dataForm'].resetFields();
      //   // this.$refs['dataForm'].clearValidate();
      //   // this.$refs[dataForm].resetFields();
      //   // this.$refs[dataForm].clearValidate();
      console.log('clearForm')
      this.$refs.dataForm.clearValidate()
    }
    /* formatTooltip */
    vue.prototype.formatTooltip = function (val) {
      return val / 100
    }
    // vue.prototype.clearForm = function () {
    //   for (let field in this.$refs) {
    //     if (this.$refs[field].resetFields) {
    //       this.$refs[field].resetFields();
    //     } else if (this.$refs[field].clearImg) {
    //       this.$refs[field].clearImg();
    //     }
    //   }
    // };
    /* 确认警告弹窗 */
    vue.prototype.showConfirmDialog = function (title, confirmCallBack) {
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        type: 'warning'
      }).then(() => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // });
        confirmCallBack()
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      })
    }
    /* *  返回 xxxx-xx-xx  */
    vue.prototype.formatTimeYMD = function (timestamp, sep = '-') {
      if (!timestamp) {
        return ''
      }
      let time = parseInt(timestamp)
      if (typeof time === 'number') {
        time = new Date(time)
        let year = time.getFullYear()
        let mon = time.getMonth() + 1
        let date = time.getDate()
        mon = mon > 9 ? mon : '0' + mon
        date = date > 9 ? date : '0' + date
        return `${year}${sep}${mon}${sep}${date}`
      } else {
        return timestamp
      }
    }
    /**  YMD
     *  返回 xxxx-xx-xx hh:mm:ss时间戳
     */
    // 转换时间戳
    vue.prototype.formatTimeYMDHMS = function (timestamp, sep = '-') {
      if (!timestamp) {
        return ''
      }
      let time = parseInt(timestamp)
      if (typeof time === 'number') {
        time = new Date(time)
        let year = time.getFullYear()
        let mon = time.getMonth() + 1
        let date = time.getDate()
        let hour = time.getHours()
        let min = time.getMinutes()
        let sec = time.getSeconds()
        mon = mon > 9 ? mon : '0' + mon
        date = date > 9 ? date : '0' + date
        hour = hour > 9 ? hour : '0' + hour
        min = min > 9 ? min : '0' + min
        sec = sec > 9 ? sec : '0' + sec
        // return `${year}${sep}${mon}${sep}${date}  ${hour}:${min}`;
        return `${year}${sep}${mon}${sep}${date} ${hour}:${min}:${sec}`
      } else {
        return timestamp
      }
    }
    // 转换提交表单
    vue.prototype.formData = function (obj) {
      let data = new FormData()
      for (let k in obj) {
        data.append(k, obj[k])
      }
      return data
    }
    vue.prototype.showInfoToast = function (msg) {
      this.$message({
        showClose: true,
        message: msg,
        type: 'info'
      })
    }
    vue.prototype.showWarnToast = function (msg) {
      this.$message({
        showClose: true,
        message: msg,
        type: 'warning'
      })
    }
    /**
     * 错误提示
     */
    vue.prototype.showErrorToast = function (msg) {
      this.$message({
        showClose: true,
        message: msg,
        type: 'error'
      })
    }
    vue.prototype.formatUrl = function (url) {
      if (StringTool.isEmpty(url)) {
        return ''
      }
      if (url.startsWith('http') || url.startsWith('https')) {
        return url
      } else {
        return this.baseImgUrl + url
      }
    }
    /**
     * 成功提示
     */
    // vue.prototype.showSuccessToast = function (msg) {
    //   this.$message({
    //     showClose: true,
    //     message: msg,
    //     type: 'success'
    //   })
    // };
    vue.prototype.showSuccessToast = function (msg, confirmCallBack) {
      this.$message({
        showClose: true,
        message: msg,
        type: 'success',
        duration: 1500,
        onClose: () => {
          if (confirmCallBack != null) {
            confirmCallBack()
          }
        }
      })
    }
    vue.prototype.hasClass = function (obj, cls) {
      return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
    }
    vue.prototype.addClass = function (obj, cls) {
      if (!this.hasClass(obj, cls)) {
        obj.className += ' ' + cls
      }
    }
    vue.prototype.removeClass = function (obj, cls) {
      if (this.hasClass(obj, cls)) {
        let reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
        obj.className = obj.className.replace(reg, ' ')
      }
    }
    // vue.prototype.showToast = function (msg) {
    //   this.$toast(msg, {className: 'seat-toast', horizontalPosition: 'center', verticalPosition: 'bottom', duration: 2000})
    // }
    // /**
    //  * 列表便捷方法
    //  */
    // Array.prototype.remove = function (el) {
    //   let index = this.indexOf(el)
    //   if (index === -1) {
    //     return null
    //   }
    //   return this.splice(index, 1)[0]
    // }
    // Array.prototype.copy = function () {
    //   return this.concat([])
    // };
    // Array.prototype.removeFromArray = function (elList) {
    //   for (let el of elList) {
    //     let index = this.indexOf(el)
    //     if (index === -1) {
    //       continue
    //     }
    //     this.splice(index, 1)
    //   }
    // }
    // Array.prototype.contain = function (el) {
    //   return this.indexOf(el) !== -1;
    // }
  }
}
